package com.small.nacos.core.distributed.distro.task;

import com.small.nacos.core.distributed.distro.component.DistroComponentHolder;
import com.small.nacos.core.distributed.distro.task.delay.DistroDelayTaskExecuteEngine;
import com.small.nacos.core.distributed.distro.task.delay.DistroDelayTaskProcessor;
import com.small.nacos.core.distributed.distro.task.execute.DistroExecuteTaskExecuteEngine;
import com.small.nacos.naming.consistency.KeyBuilder;
import org.springframework.stereotype.Component;

/**
 * distro任务中心(管理任务执行器)
 *
 * @Author zhoujin
 * @Date 2022/11/12 10:56
 */
@Component
public class DistroTaskEngineHolder {

    //distro延误任务执行器
    private final DistroDelayTaskExecuteEngine delayTaskExecuteEngine;

    //distro任务执行器
    private final DistroExecuteTaskExecuteEngine executeWorkersManager;

    private DistroComponentHolder distroComponentHolder;

    public DistroTaskEngineHolder(DistroComponentHolder distroComponentHolder) {
        this.distroComponentHolder = distroComponentHolder;
        this.delayTaskExecuteEngine = new DistroDelayTaskExecuteEngine();
        this.executeWorkersManager = new DistroExecuteTaskExecuteEngine();
        registerDistroDelayTaskProcessor();
    }

    /**
     * 获取延时任务中心
     *
     * @return
     */
    public DistroDelayTaskExecuteEngine getDelayTaskExecuteEngine() {
        return delayTaskExecuteEngine;
    }

    /**
     * 获取任务中心
     *
     * @return
     */
    public DistroExecuteTaskExecuteEngine getExecuteWorkersManager() {
        return executeWorkersManager;
    }

    /**
     * 注册默认延时的执行器
     */
    private void registerDistroDelayTaskProcessor() {
        delayTaskExecuteEngine.addProcessor(KeyBuilder.INSTANCE_LIST_KEY_PREFIX,
                new DistroDelayTaskProcessor(this, distroComponentHolder));
    }

}
